package wenjie.chen.feign;

import lombok.Data;
import lombok.experimental.Accessors;
import org.hibernate.validator.constraints.Range;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import wenjie.chen.transfer.command.TransferCommand;

import javax.validation.constraints.NotNull;

@FeignClient(value = "RECEIVER")
public interface IAccountCommandHandler {

    @RequestMapping(method = RequestMethod.POST, value = "/increaseAmount")
    void increaseAmount(@RequestBody IncreaseAmountCommand increaseAmountCommand);

    @Data
    @Accessors(chain = true)
    class IncreaseAmountCommand {
        @NotNull
        String accountId;
        @Range
        double amount;

        public static IncreaseAmountCommand of(TransferCommand transferCommand) {
            return new IncreaseAmountCommand()
                .setAccountId(transferCommand.getReceiverAccountId())
                .setAmount(transferCommand.getAmount());
        }
    }
}
